<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/css/font.css">
    <link rel="stylesheet" href="/css/xadmin.css">
    <script type="text/javascript" src="/js/jquery-3.2.1.min.js"></script>
    <script src="/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/js/xadmin.js"></script>
</head>
<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a>
          <cite>用户管理</cite></a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
    <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so" action="/userInfo/list">
            <input type="text" name="userName"  placeholder="请输入用户名" autocomplete="off" class="layui-input">
            <input type="text" name="email"  placeholder="请输入邮箱" autocomplete="off" class="layui-input">
            <button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
        </form>
    </div>
    <xblock>
        <form action="/userInfo/excelWrite" >
            <button class="layui-btn layui-btn-danger" onclick="delAll()" type="button"><i class="layui-icon"></i>批量删除</button>
            <button class="layui-btn" onclick="x_admin_show('添加用户','/userInfo/add')" type="button"><i class="layui-icon"></i>用户添加</button>
            <button class="layui-btn" onclick="x_admin_show('添加用户','/userInfo/add')" type="button"><i class="layui-icon"></i>添加</button>
            <button class="layui-btn" type="submit"><i class="layui-icon"></i>excel导出</button>
        </form>

        <span class="x-right" style="line-height:40px">共有数据：<span th:text="${totalElments}"></span> 条</span>
        <span class="x-right" style="line-height:40px">共有：<span th:text="${totalPages}"></span> 页</span>
    </xblock>
    <table class="layui-table">
        <thead>
        <tr>
            <th>
                <div class="layui-unselect header layui-form-checkbox" lay-skin="primary"><i class="layui-icon">&#xe605;</i></div>
            </th>
            <th>ID</th>
            <th>登录名</th>
            <th>手机</th>
            <th>邮箱</th>
            <th>角色</th>
            <th>加入时间</th>
            <th>状态</th>
            <th>操作</th>
        </thead>
        <tbody>
        <tr th:each="x:${list}">
            <td>
<!--                <div class="layui-unselect layui-form-checkbox" lay-skin="primary" th:data-id='${x.uId}'><i class="layui-icon">&#xe605;</i></div>-->
                <input type="checkbox" th:value="${x.uId}" th:udid="${x.userDetail.udId}" >
            </td>

            <td th:text="${x.uId}"></td>
            <td th:text="${x.userName}"></td>
            <td th:text="${x.userDetail.tel}"></td>
            <td th:text="${x.userDetail.email}"></td>
            <td>
                <ul th:each="y:${x.roleInfoList}" style="float: left;margin-left:4px">
                    <li th:text="${y.rName}"></li>
                </ul>
            </td>
            <td th:text="${x.userDetail.joinTime}"></td>
            <td class="td-status" th:if="${x.userDetail.status==1}">
                <span class="layui-btn layui-btn-normal layui-btn-mini">启用</span>
            </td>
            <td class="td-status" th:if="${x.userDetail.status==0}">
                <span class="layui-btn layui-btn-normal layui-btn-mini layui-btn-disabled">停用</span>
            </td>
            <td class="td-manage" th:if="${x.userDetail.status==1}">
                <a onclick="member_stop(this)" href="javascript:;"  title="启用">
                    <i class="layui-icon">&#xe601;</i>
                </a>
                <a title="编辑"  onclick="update(this)" href="javascript:;">
                    <i class="layui-icon">&#xe642;</i>
                </a>
                <a title="删除" onclick="member_del(this)" href="javascript:;">
                    <i class="layui-icon">&#xe640;</i>
                </a>
            </td>
            <td class="td-manage" th:if="${x.userDetail.status==0}">
                <a onclick="member_stop(this)" href="javascript:;"  title="停用">
                    <i class="layui-icon">&#xe601;</i>
                </a>
                <a title="编辑"  onclick="update(this)" href="javascript:;">
                    <i class="layui-icon">&#xe642;</i>
                </a>
                <a title="删除" onclick="member_del(this)" href="javascript:;">
                    <i class="layui-icon">&#xe640;</i>
                </a>
            </td>
        </tr>
        </tbody>
    </table>
    <div class="page">
        <div>
            <a class="prev" th:href="@{/userInfo/list(page=${pre})}">&lt;&lt;</a>
            <a class="num" th:href="@{/userInfo/list(page=1)}">首页</a>
            <span class="current" th:text="${cur}"></span>
            <a class="num" th:href="@{/userInfo/list(page=${last})}">尾页</a>
            <a class="next" th:href="@{/userInfo/list(page=${next})}">&gt;&gt;</a>
        </div>
    </div>
</div>
</body>

</html>
<script>
    /*用户-停用*/
    function member_stop(obj){
        //获取用户的uid
        var uId=$(obj).parents("tr").find("td").eq(1).text();
        console.log("uId="+uId);

        layer.confirm('确认要更改状态吗？',function(index){
            $.ajax({
                url:"/userInfo/updateStatus",
                type:"get",
                dataType:"json",
                data:{"uId":uId},
                success:function(data){
                    if(data==true){
                        if($(obj).attr('title')=='启用'){
                            //发异步把用户状态进行更改
                            $(obj).attr('title','停用')
                            $(obj).find('i').html('&#xe62f;');
                            $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('停用');
                            layer.msg('已停用!',{icon: 5,time:1000});

                        }else{
                            $(obj).attr('title','启用')
                            $(obj).find('i').html('&#xe601;');

                            $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('启用');
                            layer.msg('已启用!',{icon: 5,time:1000});
                        }
                    }else{
                        layer.msg('更改状态失败',{icon: 5,time:1000});
                    }
                }
            })

        });
    }


    //编辑
    function update(obj){
        //获取当前选中行用户的uId
        var uId = $(obj).parents("tr").find("td").eq(1).text();
        console.log("uId="+uId)
        //打开窗体
        x_admin_show("修改用户","/userInfo/update?uId="+uId);
    }

    /*用户-删除*/
    function member_del(obj){
        //获取要删除用户的编号uId
        var uId=$(obj).parents("tr").find("td").eq(1).text();
        layer.confirm('确认要删除吗？',function(index){
            $.ajax({
                url:"/userInfo/del",
                async: false,
                type:"POST",
                dataType: "json",
                data:{"uId":uId},
                success: function(data){
                    if(data==true){
                        $(obj).parents("tr").remove();
                        layer.msg('已删除!',{icon:1,time:1000});
                    }else{
                        layer.msg('删除失败!',{icon: 1,time:1000});
                    }

                }
            })



        });
    }
    //批量删除
    function delAll () {
        //获取复选框选中的用户的uId
        var uIds = "";
        var udIds=""
        $('input[type=checkbox]:checked').each(function () {
            uIds += $(this).val() + ",";
            //获取自定义的属性值
            udIds+=$(this).attr("udid")+","
            //给选中的复选框的tr 加样式
            $(this).parents("tr").addClass("dddd");
        });
        console.log(uIds)

        layer.confirm('确认要删除吗？'+data,function(index){
            $.ajax({
                url:"/userInfo/delAll",
                async: false,
                type:"POST",
                dataType: "json",
                data:{"uIds":uIds,"udIds":udIds},
                success: function(data){
                    if(data==true){
                        layer.msg('删除成功', {icon: 1});
                        //删除选中tr
                        $(".dddd").remove();
                    }else{
                        layer.msg('删除失败!',{icon: 1,time:1000});
                    }

                }
            })

        });
    }
</script>